package com.meilishuo.message.util.SyncData;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.meilishuo.message.service.SynOrderDetailService;
import com.meilishuo.message.service.Impl.SynOrderDetailServiceImpl;
import com.meilishuo.message.util.entity.Address;
import com.meilishuo.message.util.entity.Order.OrderList;
import com.meilishuo.message.util.entity.Order.OrderListOrder;
import com.meilishuo.message.util.entity.Order.OrderRequest;
import com.meilishuo.message.util.entity.Order.OrderService;
import com.meilishuo.message.util.request.OrderListRequest;
import com.meilishuo.message.util.respon.OrderListResponse;

/**
 * Created by chenhting on 15/5/10.
 */
public class SyncOrderList {
	
	@Autowired
	SynOrderDetailService synOrderDetailService;
	
    public void sycOrderList(OrderListRequest request,String session){
    	
    	OrderListResponse orderListResponse=new OrderListResponse();
    	OrderRequest orderRequest=new OrderRequest();
    	List<OrderList> orderLists=new ArrayList<OrderList>();
    	String result= orderListResponse.getOrderList(request, session);
    	if (result != "false") {
			orderRequest=JSON.parseObject(result,OrderRequest.class);
			orderLists=orderRequest.getInfo();
		}
    	double total_num=orderRequest.getTotal_num();
    	if (total_num <= 100) {
    		for (OrderList orderList : orderLists) {
				OrderListOrder order= orderList.getOrder();
				Address address=orderList.getAddress();
				List<OrderService> services=orderList.getService();
				synOrderDetailService.insertOrderDetail(order, address, services);
			}
		}else {
			int pageSize=(int)Math.ceil(total_num/100);
			for (int i = 1; i < pageSize; i++) {
				request.setPage(i);
				result=orderListResponse.getOrderList(request, session);
				if (result != "false") {
					orderRequest=JSON.parseObject(result,OrderRequest.class);
					orderLists=orderRequest.getInfo();
					for (OrderList orderList : orderLists) {
						OrderListOrder order=orderList.getOrder();
						Address address=orderList.getAddress();
						List<OrderService> services=orderList.getService();
						synOrderDetailService.insertOrderDetail(order, address, services);
					}
				}
			}
		}
    }
}
